<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="记录编码" prop="finaCode">
        <el-input v-model="queryParams.finaCode" placeholder="请输入记录编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="日期" prop="finaDate">
        <!-- <el-date-picker clearable v-model="queryParams.finaDate" type="date" value-format="yyyy-MM-dd" placeholder="日期">
        </el-date-picker> -->
        <el-date-picker type="daterange" v-model="finaDate" format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期"
                  range-separator="至" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="记账类型" prop="subId1">
        <!-- <el-input v-model="queryParams.subName1" placeholder="请输入记账类型" clearable @keyup.enter.native="handleQuery" /> -->
      <el-select v-model="queryParams.subId1" placeholder="请选择记账类型"  clearable @keyup.enter.native="handleQuery"  @change="getSubListBySubIdSearch(2,queryParams.subId1)">
        <el-option
            v-for="item in sub1Options"
            :key="item.subId"
            :label="item.subName"
            :value="item.subId"
            :disabled="item.delFlag == 'N'"
          ></el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="一级科目" prop="subId2">
        <!-- <el-input v-model="queryParams.subName2" placeholder="请输入一级科目" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.subId2" placeholder="请选择一级科目"  clearable @keyup.enter.native="handleQuery" @change="getSubListBySubIdSearch(3,queryParams.subId2)">
          <el-option
              v-for="item in sub2Options"
              :key="item.subId"
              :label="item.subName"
              :value="item.subId"
              :disabled="item.delFlag == 'N'"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级科目" prop="subId3">
        <el-select v-model="queryParams.subId3" placeholder="请选择二级科目"  clearable @keyup.enter.native="handleQuery" @change="getSubListBySubIdSearch(4,queryParams.subId3)">
          <el-option
              v-for="item in sub3Options"
              :key="item.subId"
              :label="item.subName"
              :value="item.subId"
              :disabled="item.delFlag == 'N'"
            ></el-option>
        </el-select>
      </el-form-item>
      </el-form-item>


      <el-form-item label="用途" prop="finaAbstractName">
        <el-input v-model="queryParams.finaAbstractName" placeholder="请输入用途" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="摘要" prop="remark">
        <el-input v-model="queryParams.remark" placeholder="请输入摘要" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="客户名称" prop="cusName">
        <el-input v-model="queryParams.cusName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="经办人" prop="finaOperatorId">
        <el-select v-model="queryParams.finaOperatorId"  placeholder="请输入经办人" clearable
          @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.cash_opter"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="付款日期" prop="finaPayDate">
        <el-date-picker type="daterange" v-model="finaPayDate" format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="付款开始日期" end-placeholder="付款结束日期"
                  range-separator="至" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:fina:cash:exportDetail']"
        >导出</el-button>
      </el-col>
    </el-row>


        <el-table v-loading="loading" :data="cashBalanceList"  :cell-style="cellStyle" show-summary :summary-method="getSummaries">
          <!-- <el-table-column label="记录编码" align="center" prop="finaCode" /> -->
          <el-table-column label="日期" align="center" prop="finaDate" sortable width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.finaDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="记账类型" align="center" prop="subName1" />
          <el-table-column label="一级科目" align="center" prop="subName2" />
          <el-table-column label="二级科目" align="center" prop="subName3" />
          <el-table-column label="用途" align="center" prop="finaAbstractName" />
          <!-- <el-table-column label="客户名称" align="center" prop="cusName" /> -->
          <el-table-column label="经办人" align="center" prop="finaOperatorName" />
          <el-table-column label="摘要" align="center" prop="remark" />
          <el-table-column label="收入" align="center" prop="finaIn" />
          <el-table-column label="支出" align="center" prop="finaOut" />
          <el-table-column label="结余" align="center" prop="finaBalance" />
          <el-table-column label="付款日期" align="center" prop="finaPayDate" />
        </el-table>
<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />




   </div>
</template>

<script>
  import { listCash, getCash, delCash, addCash, updateCash,balanceList,balanceAllList,countList } from "@/api/fina/cash";
  import { listSub,listByOrder } from "@/api/fina/sub";
  import {genCode} from "@/api/system/autocode/rule"

  export default {
    name: "Cash",
    dicts: ['cash_opter'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 出纳管理列表格数据
        cashList: [],
        //日期范围
        finaDate: [],
        //日期范围
        finaPayDate: [],
        //出纳统计汇总
        cashBalanceList: [],
        //经办人统计汇总
        countList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 收入显示
        inShow:true,
        // 支出显示
        outShow:true,
        // 用途显示
        finaAbstractShow:false,
        // 客户名称显示
        cusNameShow:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          finaCode: null,
          finaDate: null,
          finaDateStart: null,
          finaDateEnd: null,
          subId1: null,
          subName1:null,
          subId2: null,
          subName2:null,
          subId3: null,
          subName3:null,
          subId4: null,
          subName4:null,
          orderNum: null,
          finaAbstract: null,
          finaOperatorId: null,
          finaOperatorName: null,
          finaIn: null,
          finaOut: null,
          finaBalance: null,
          status: null,
          finaPayDate: null,
          finaPayDateStart: null,
          finaPayDateEnd: null,
        },
        // 表单参数
        form: {},
        //弹框的操作类型 view add edit
        optType: undefined,
        //自动生成物料编码标识
        autoGenFlag: false,
        //是否海洋科技
        opteratorIsHY:false,
        //所有科目列表
        subAllOptions:[],
        //科目一列表
        sub1Options: [],
        //科目二列表
        sub2Options: [],
        //科目三列表
        sub3Options: [],
        //科目四列表
        sub4Options: [],
        //科目四列表
        finaAbstractOptions: [],
        //科目一选中id
        sub1IdSel:0,
        //科目2选中id
        sub2IdSel:0,
        //科目3选中id
        sub3IdSel:0,
        //科目4选中id
        sub4IdSel:0,
        //经办人总列表
        cashOpter: [],
        //经办人使用列表
        cashOpterUse: [],
        //经办人-1号仓
        cashOpter1: [],
        //经办人-2号仓
        cashOpter2: [],
        //经办人-借还仓
        cashOpter3: [],
        spanTableRow: {},
        allList: [],

        // 表单校验
        rules: {
          finaCode: [
            { required: true, message: "记录编码不能为空", trigger: "blur" },
            { max: 64, message: '记录编码长度必须小于64个字符', trigger: 'blur' }
          ],
          finaDate: [
            { required: true, message: "日期不能为空", trigger: "blur" }
          ],
          subId1: [
            { required: true, message: "记账类型不能为空",trigger: "blur"}
          ],
          subId2: [
            { required: true, message: "一级科目不能为空", trigger: "blur" },
          ],
          subId3: [
            { required: true, message: "二级科目不能为空",trigger: "blur"}
          ],
          subId4: [
            { required: true, message: "四级科目不能为空", trigger: "blur" },
          ],
          finaAbstract: [
            { required: true, message: "用途不能为空",trigger: "blur"}
          ],
          finaOperatorName: [
            { required: true, message: "经办人不能为空", trigger: "blur" },
          ],
          finaIn: [
            { required: false, message: "请录入收入金额(精确小数点2为)", trigger: "blur" },
          ],
          finaOut: [
            { required: false, message: "请录入支出金额(精确小数点2为)", trigger: "blur" },
          ],
          // finaIn: [
          //   { required: false, validator:this.isInMoney.bind(this) ,message: "请录入收入金额(精确小数点2为)", trigger: "blur" },
          // ],
          // finaOut: [
          //   { required: false, validator:this.isOutMoney.bind(this) ,message: "请录入支出金额(精确小数点2为)", trigger: "blur" },
          // ],
          cusName: [
            { required: false, message: "客户名称不能为空", trigger: "blur" },
          ],

        }
      };
    },
    created() {
      this.getList();
      this.getSubList();
    },
    methods: {
      /** 查询出纳管理列列表 */
      getList() {
        this.loading = true;
        if(this.finaDate != undefined && this.finaDate != null && this.finaDate.length>0){
          // console.log("finaDate>>>",this.finaDate)
          this.queryParams.finaDateStart = this.finaDate[0]
          this.queryParams.finaDateEnd = this.finaDate[1]
        }
        if(this.finaPayDate != undefined && this.finaPayDate != null && this.finaPayDate.length>0){
          // console.log("finaDate>>>",this.finaDate)
          this.queryParams.finaPayDateStart = this.finaPayDate[0]
          this.queryParams.finaPayDateEnd = this.finaPayDate[1]
        }


        /** 获取统计报表 */
        balanceList(this.queryParams).then(response => {
          // console.log("cashBalanceList>>>",JSON.stringify(response))
          this.cashBalanceList = response.rows;
          this.total = response.total;
          this.loading = false;
          // console.log("cashBalanceList>>>",JSON.stringify(this.cashBalanceList))
        });

        /** 获取统计报表 */
        balanceAllList(this.queryParams).then(response => {
          this.allList = response.data
        });
      },
      //经办人统计算法
      getCountList(data){
        var balance1 = 0.00 //余额1
        var balance2 = 0.00 //余额2
        var balance3 = 0.00 //余额3

        var countListTemp = []
        var countItem0 = {"finaCode":"海洋科技","accountBalance":0.00,"subName1":"短期借款","balance":0.00}
        var countItem1 = {"finaCode":"MK9813","accountBalance":0.00,"subName1":"觅康工商","balance":0.00}
        var countItem2 = {"finaCode":"MK5255","accountBalance":0.00,"subName1":"觅康农商","balance":0.00}
        var countItem3 = {"finaCode":"MK222","accountBalance":0.00,"subName1":"觅康膳臻","balance":0.00}
        var countItem4 = {"finaCode":"MK收款码","accountBalance":0.00,"subName1":"觅康收款码","balance":0.00}
        var countItem5 = {"finaCode":"SMK6217","accountBalance":0.00,"subName1":"觅康2号仓","balance":0.00}
        var countItem6 = {"finaCode":"MKXJ","accountBalance":0.00,"subName1":"觅康2号仓","balance":0.00}
        var countItem7 = {"finaCode":"JMXJ","accountBalance":0.00,"subName1":"短期借款","balance":0.00}
        var countItem8 = {"finaCode":"SJM3649","accountBalance":0.00,"subName1":"短期借款","balance":0.00}
        var countItem9 = {"finaCode":"匠觅公司","accountBalance":0.00,"subName1":"短期借款","balance":0.00}

        if(data!=undefined && data!=null && data.length>0){
          for(let i=0;i<data.length;i++){
            var item = data[i]
            var accountBalanceTemp = item.finaBalance
            if(item != undefined && item != null){
              if(item.finaOperatorName == "海洋科技"){
                countItem0.accountBalance = countItem0.accountBalance+accountBalanceTemp
                balance1 = balance1 + accountBalanceTemp
              }
              if(item.finaOperatorName == "MK9813"){
                countItem1.accountBalance = countItem1.accountBalance+accountBalanceTemp
                balance2  = balance2 + accountBalanceTemp
              }
              if(item.finaOperatorName == "MK5255"){
                countItem2.accountBalance = countItem2.accountBalance+accountBalanceTemp
                balance2  = balance2 + accountBalanceTemp
              }
              if(item.finaOperatorName == "MK222"){
                countItem3.accountBalance = countItem3.accountBalance+accountBalanceTemp
                balance2  = balance2 + accountBalanceTemp
              }
              if(item.finaOperatorName == "MK收款码"){
                countItem4.accountBalance = countItem4.accountBalance+accountBalanceTemp
                balance2  = balance2 + accountBalanceTemp
              }
              if(item.finaOperatorName == "SMK6217"){
                countItem5.accountBalance = countItem5.accountBalance+accountBalanceTemp
                balance2  = balance2 + accountBalanceTemp
              }
              if(item.finaOperatorName == "MKXJ"){
                countItem6.accountBalance = countItem6.accountBalance+accountBalanceTemp
                balance2  = balance2 + accountBalanceTemp
              }
              if(item.finaOperatorName == "JMXJ"){
                countItem7.accountBalance = countItem7.accountBalance+accountBalanceTemp
                balance3  = balance3 +accountBalanceTemp
              }
              if(item.finaOperatorName == "SJM3649"){
                countItem8.accountBalance = countItem8.accountBalance+accountBalanceTemp
                balance3  = balance3 + accountBalanceTemp
              }
              if(item.finaOperatorName == "匠觅公司"){
                countItem9.accountBalance = countItem9.accountBalance+accountBalanceTemp
                balance3  = balance3 + accountBalanceTemp
              }
            }
          }
          this.$set(countItem0,"balance",balance1)
          this.$set(countItem1,"balance",balance2)
          this.$set(countItem2,"balance",balance2)
          this.$set(countItem3,"balance",balance2)
          this.$set(countItem4,"balance",balance2)
          this.$set(countItem5,"balance",balance2)
          this.$set(countItem6,"balance",balance2)
          this.$set(countItem7,"balance",balance3)
          this.$set(countItem8,"balance",balance3)
          this.$set(countItem9,"balance",balance3)

          countListTemp.push(countItem0)
          countListTemp.push(countItem1)
          countListTemp.push(countItem2)
          countListTemp.push(countItem3)
          countListTemp.push(countItem4)
          countListTemp.push(countItem5)
          countListTemp.push(countItem6)
          countListTemp.push(countItem7)
          countListTemp.push(countItem8)
          countListTemp.push(countItem9)

        }
        console.log("countListTemp>>>",countListTemp)
        return countListTemp
      },

      getSummaries(res){
        const { columns, data } = res
        // console.log("this.allList>>>>",this.allList);
        const sum = []

        columns.forEach((element, index) => {
          if (index == 1) {
            sum[index] = '合计'
          }
          if (index == 7) {
            //cur.wxAmount  合计对象的数字
             sum[index] = this.allList.reduce((pre, cur) => pre + Number(cur.finaIn), 0)
             sum[index] = sum[index].toFixed(2)
          }
          if (index == 8) {
            //cur.wxAmount  合计对象的数字
             sum[index] = this.allList.reduce((pre, cur) => pre + Number(cur.finaOut), 0)
             sum[index] = sum[index].toFixed(2)
          }
          if (index == 9) {
            //cur.wxAmount  合计对象的数字
             sum[index] = this.allList.reduce((pre, cur) => pre + Number(cur.finaBalance), 0)
             sum[index] = sum[index].toFixed(2)
          }
        });
        return sum

      },
      /** 获取科目下拉列表 */
      getSubList(){
        listSub(null).then(response => {
          // debugger;
          var data = response.rows
          this.subAllOptions = data
          // console.log("data::::",data)
          for(let i=0;i<data.length;i++){
            var item = data[i]
            // console.log("item::::",item)
            var orderNum = item.orderNum
            if(orderNum == 1){
              this.sub1Options.push(item)
            }
            else if(orderNum == 2){
              this.sub2Options.push(item)
            }else if(orderNum == 3){
              this.sub3Options.push(item)
            }else if(orderNum == 4){
              this.sub4Options.push(item)
            }
          }
        });



      },
      /** 根据上一级获取对应下一级列表 */
      getSubListBySubId(row,subId){
        var data = this.subAllOptions
        var subOptions = []
        var subName = ""
        for(let i=0;i<data.length;i++){
          var item = data[i]
          if(item.subId == subId){
            subName = item.subName
          }
          // console.log("item::::",item)
          var orderNum = item.orderNum
          if(row == 2){
            if(item.subId == subId){
              this.form.subName1 = item.subName
            }
          }else if(row == 3){
            if(item.subId == subId){
              this.form.subName2 = item.subName
            }
          }else if(row == 4){
            if(item.subId == subId){
              this.form.subName3 = item.subName
              if(item.subName == "海洋科技"){
                this.form.finaOperatorId = 1001
                this.form.finaOperatorName = "海洋科技"
                // this.form.finaOperatorName = {"key":1001,"label":"海洋科技","value":1001}
                this.opteratorIsHY = true
                this.rules.finaOperatorName[0].required = false
              }
              else{
                this.opteratorIsHY = false
              //   this.form.finaOperatorName = ""
                this.rules.finaOperatorName[0].required = true
              }
            }

          }else if(row == 5){
              if(item.subId == subId){
                this.form.subName4 = item.subName
              }
          }
          if(item.parentId == subId){
            subOptions.push(item)
          }

        }

        if(row == 2){

          if(subId == 101){//应收
            this.inShow = true
            this.outShow = false
            this.rules.finaIn[0].required = true
            this.rules.finaOut[0].required = false
          }else if(subId == 102){// 应付
            this.inShow = false
            this.outShow = true
            this.rules.finaIn[0].required = false
            this.rules.finaOut[0].required = true
          }

          this.form.subId2 = null
          this.form.subId3 = null
          this.form.subId4 = null
          this.form.finaAbstract = null
          this.sub2Options = subOptions
        }else if(row == 3){
          this.form.subId3 = null
          this.form.subId4 = null
          this.form.finaAbstract = null
          this.sub3Options = subOptions
          //设置经办人
          //初始化经办人
          var cashOpter = this.dict.type.cash_opter
          this.cashOpterUse = []
          this.cashOpter1 = []
          this.cashOpter2 = []
          this.cashOpter3 = []
          var cashOpterTemp = []
            // console.log("opt>>>>>",JSON.stringify(cashOpter.length))
          for(let i=0;i<cashOpter.length;i++){
            var opt = cashOpter[i]

            console.log("opt.label>>>>>",opt.label)
            if(opt.label === "MK9813"){
              this.cashOpter1.push(opt)
            }
            if(opt.label === "MK5255"){
              this.cashOpter1.push(opt)
            }
            if(opt.label === "MK222"){
              this.cashOpter1.push(opt)
            }
            if(opt.label === "MK收款码"){
              this.cashOpter1.push(opt)
            }

            if(opt.label === "SMK6217"){
              this.cashOpter2.push(opt)
            }
            if(opt.label === "SJM3649"){
              this.cashOpter2.push(opt)
            }
            if(opt.label === "MKXJ"){
              this.cashOpter2.push(opt)
            }
            if(opt.label === "JMXJ"){
              this.cashOpter2.push(opt)
            }


            if(opt.label === "海洋科技" || opt.label === "匠觅公司"){
              this.cashOpter3.push(opt)
            }
          }


          // console.log("cashOpter1>>>>>",cashOpterTemp)
          // console.log("cashOpter1>>>>>",this.cashOpter1)
          // console.log("cashOpter2>>>>>",this.cashOpter2)
          // console.log("cashOpter1>>>>>",this.cashOpter3)


          if(subName.indexOf("1号") != -1){
            this.cashOpterUse = this.cashOpter1
          }else if(subName.indexOf("2号") != -1){
            this.cashOpterUse = this.cashOpter2
          }else if(subName.indexOf("借") != -1 || subName.indexOf("还") != -1){
            this.cashOpterUse = this.cashOpter3
          }

          // console.log("cashOpterUse>>>>>",this.cashOpterUse)

        }else if(row == 4){
          this.form.subId4 = null
          this.form.finaAbstract = null
          this.sub4Options = subOptions
        }else if(row == 5){
          this.form.finaAbstract = null
          this.finaAbstractOptions = subOptions
          // console.log("this.finaAbstractShow",this.finaAbstractShow)
          if(subOptions != null && subOptions.length > 0){//存在用途
          // console.log("subOptions",subOptions,"存在用途")
            this.finaAbstractShow = true
            this.rules.finaAbstract[0].required = true
          }else{// 不需要录入用途
          // console.log("subOptions",subOptions,"不需要录入用途")
            this.finaAbstractShow = false
            this.rules.finaAbstract[0].required = false
          }
          //判断是否存在需要录入客户名称

          if(this.form.subName4 != undefined && this.form.subName4 != null && this.form.subName4 != ""){
            var subNameTemp = this.form.subName4;
            // console.log("subNameTemp",subNameTemp)
            // console.log("this.cusNameShow",this.cusNameShow)
            if(subNameTemp.indexOf("客户") != -1){
            // console.log("subNameTemp",subNameTemp,"需要录入客户")
              this.cusNameShow = true
              this.rules.cusName[0].required = true
            }else{
            // console.log("subNameTemp",subNameTemp,"不需要录入客户")
              this.cusNameShow = false
              this.rules.cusName[0].required = false
            }
          }

        }

      },

      /** 搜索根据上一级获取对应下一级列表 */
      getSubListBySubIdSearch(row,subId){
        var data = this.subAllOptions
        var subOptions = []
        for(let i=0;i<data.length;i++){
          var item = data[i]
          var orderNum = item.orderNum
          if(item.parentId == subId){
            subOptions.push(item)
          }
        }

        if(row == 2){

          this.queryParams.subId2 = null
          this.sub2Options = subOptions
        }else if(row == 3){
          this.queryParams.subId3 = null
          this.sub3Options = subOptions
        }else if(row == 4){
          this.queryParams.subId4 = null
          this.sub4Options = subOptions
        }else if(row == 5){
          this.queryParams.finaAbstract = null
          this.finaAbstractOptions = subOptions
        }

      },
      setOperatorName(cashOpte,finaOperatorName){
        this.cashOpter = cashOpte
        // for(let i=0;i<cashOpte.length;i++){
        //   var item = cashOpte[i]
        //   if(item.value == finaOperatorName){
        //     this.form.finaOperatorId = item.value
        //     this.form.finaOperatorName = item.label
        //     break
        //   }
        // }
        // // $forceUpdate()
        // console.log("this.form.finaOperatorId>>>",this.form.finaOperatorId)
        // console.log("this.form.finaOperatorName>>>",this.form.finaOperatorName)
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          finaId: null,
          finaCode: null,
          finaDate: null,
          subId1: null,
          subName1:null,
          subId2: null,
          subName2:null,
          subId3: null,
          subName3:null,
          subId4: null,
          subName4:null,
          orderNum: null,
          finaAbstract: null,
          finaOperatorId: null,
          finaIn: null,
          finaOut: null,
          finaBalance: null,
          remark: null,
          status: "0",
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.autoGenFlag = false;
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.finaId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加日记账记录";
        this.optType = "add";
        // this.form.finaOperatorName = "海洋科技"
        // console.log(">>>>>>>>",JSON.stringify(this.dict.type.cash_opter))
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.optType = "edit";
        const finaId = row.finaId || this.ids
        getCash(finaId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改日记账记录";
          console.log("this.form>>>",this.form)
          if(this.form.subId1 == 101){//应收
            this.inShow = true
            this.outShow = false
            this.rules.finaIn[0].required = true
            this.rules.finaOut[0].required = false
          }else if(this.form.subId1 == 102){// 应付
            this.inShow = false
            this.outShow = true
            this.rules.finaIn[0].required = false
            this.rules.finaOut[0].required = true
          }


          if(this.form.subId4 != undefined && this.form.subId4 != null && this.form.subId4 != ""){

            // console.log("subOptions",subOptions,"存在用途")
            this.finaAbstractShow = true
            this.rules.finaAbstract[0].required = true

            var subNameTemp = this.form.subName4;
            // console.log("subNameTemp",subNameTemp)
            // console.log("this.cusNameShow",this.cusNameShow)
            if(subNameTemp.indexOf("客户") != -1){
            // console.log("subNameTemp",subNameTemp,"需要录入客户")
              this.cusNameShow = true
              this.rules.cusName[0].required = true
            }else{
            // console.log("subNameTemp",subNameTemp,"不需要录入客户")
              this.cusNameShow = false
              this.rules.cusName[0].required = false
            }
          }else{// 不需要录入用途
          // console.log("subOptions",subOptions,"不需要录入用途")
            this.finaAbstractShow = false
            this.rules.finaAbstract[0].required = false
          }
        });
      },
      /** 提交按钮 */
      submitForm() {
        if(this.inShow){
          if (!/^\d+(\.\d{0,2})?$/.test(this.form.finaIn)) {
            this.$modal.msgError('请输入正确的收入金额（最多只能保留两位小数）')
            return;
          }
        }

        if(this.outShow){
          if (!/^\d+(\.\d{0,2})?$/.test(this.form.finaOut)) {
            this.$modal.msgError('请输入正确的支出金额（最多只能保留两位小数）')
            return;
          }
        }

        if(this.dict != undefined && this.dict != null){
          var cashOpte = this.dict.type.cash_opter
          var finaOperatorIdTemp = this.form.finaOperatorName
          for(let i=0;i<cashOpte.length;i++){
            var item = cashOpte[i]
            if(item.value == finaOperatorIdTemp){
              this.form.finaOperatorId = item.value
              this.form.finaOperatorName = item.label
              break
            }
          }
        }
        // console.log("this.form.finaOperatorId>>>",this.form.finaOperatorId)
        // console.log("this.form.finaOperatorName>>>",this.form.finaOperatorName)
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.finaId != null) {
              updateCash(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCash(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const finaIds = row.finaId || this.ids;
        this.$modal.confirm('是否确认删除日记账记录编号为"' + finaIds + '"的数据项？').then(function() {
          return delCash(finaIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('/mes/fina/cash/exportCashCountDetail', {
          ...this.queryParams
        }, `cash_${new Date().getTime()}.xlsx`)
      },
      //自动生成物料编码
      handleAutoGenChange(autoGenFlag){
        // debugger;
        if(autoGenFlag){
          genCode('FINA_CODE').then(response =>{
            // console.log("response>>>>>>",response)
            this.form.finaCode = response;
          });
         }else{
            this.form.finaCode = null;
         }
      },
      cellStyle({ row, column, rowIndex, columnIndex }) {
        if (column.property === 'subName3' ) {
          if (row.subName3 === '海洋科技') {
            return 'color: red;';
          }
        }
        if (column.property === 'subName3' ) {
          if (row.subName3 === '海洋科技') {
            return 'color: red;';
          }
        }
        if (column.property === 'subName4' ) {
          if (row.subName4 === '海洋科技') {
            return 'color: red;';
          }
        }
        if (column.property === 'finaOperatorName' ) {
          if (row.finaOperatorName === '海洋科技') {
            return 'color: red;';
          }
        }

        // if (column.property === 'subName3' || column.property === 'finaOperatorName' || column.property === 'subName4' ) {
        //   if (row.finaOperatorName === '海洋科技' || row.subName3 === '海洋科技' || row.subName4 === '海洋科技') {
        //     return 'color: red;';
        //   }
        // }

      },
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return 'row1';
        } else if (rowIndex >= 1 && rowIndex <= 6 ) {
          return "row2";
        } else if (rowIndex >= 7 && rowIndex <= 9 ) {
          return "row3";
        }
      },
      arraySpanMethod({ row, column, rowIndex, columnIndex }) {
        console.log("columnIndex>>",columnIndex)
        if(rowIndex == 0){
           if (![0,1,2].includes(columnIndex)) {
               return [1, 1];
           }
        }
        if(rowIndex >= 1 && rowIndex <=6){
          if (![0,1,2].includes(columnIndex)) {
              return [6, 1];
          }else{
             return [1, 1];
          }
       }
       if(rowIndex >= 7 && rowIndex <=9){
         if (![0,1,2].includes(columnIndex)) {
             return [2, 1];
         }else{
           return [1, 1];
         }
       }
      },

      // 收入金额验证表达式
      isInMoney(rule,value,callback){
        const { required } = rule
          if(!required){
            console.log("无需收入>>>>>>>>")
            return
          }
          if (value === '') {
            if (required) callback('请输入收入金额')
            else callback()
          } else if (!/^\d+(\.\d{0,2})?$/.test(value)) {
            callback('请输入正确的收入数值（最多只能保留两位小数）')
          } else if (value > 10000000) {
            callback('单次收入不得大于10000000')
          } else {
            callback()
          }
      },
      // 支出金额验证表达式
      isOutMoney(rule,value,callback){
        const { required } = rule

          if(!required){
            console.log("无需支出>>>>>>>>")
            return
          }
          if (value === '') {
            if (required) callback('请输入支出金额')
            else callback()
          } else if (!/^\d+(\.\d{0,2})?$/.test(value)) {
            callback('请输入正确的支出数值（最多只能保留两位小数）')
          } else if (value > 10000000) {
            callback('单次支出不得大于10000000')
          } else {
            value = 0-value
            callback()
          }
      }
    }
  };
</script>

<style>
.el-table .row1 {
  background: #EAFAF1;
}
.el-table .row2 {
  background: #FDEDEC;
}
.el-table .row3 {
  background: #EBF5FB;
}
</style>
